package com.ruoyi.base.utils.arrays;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 数组工具类
 *
 * @author lcf
 */
public class ArrayUtils {

    /**
     * 数组转list
     */
    public static <T> List<T> arrays2List(T[] arrays) {
        if (arrays == null || arrays.length == 0) {
            return Collections.emptyList();
        }

        return new ArrayList<>(Arrays.asList(arrays));
    }

    /**
     * 判断数组是否为空
     */
    public static <T> boolean isEmpty(T[] arrays) {
        return arrays == null || arrays.length == 0;
    }

    /**
     * 判断数组是否为空
     */
    public static <T> boolean isNotEmpty(T[] arrays) {
        return !isEmpty(arrays);
    }

    /**
     * 拼接字符串
     */
    public static <T> String join(Collection<T> list, String separator) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }

        separator = StringUtils.isBlank(separator) ? "," : separator;

        StringBuilder result = new StringBuilder();
        for (T s : list) {
            result.append(s).append(separator);
        }
        return result.substring(0, result.length() - 1);
    }

    /**
     * 拼接字符串
     */
    public static <T> String join(Collection<T> list) {
        return join(list, null);
    }
}
